#include "StudentEditor.h"

StudentEditor::StudentEditor( QWidget* parent )
	: QWidget( parent )
	, mStudent( NULL )
{
	// Layout
	QGridLayout* layout = new QGridLayout();
	setLayout( layout );

	// Name
	QFormLayout* nameLayout = new QFormLayout();
	layout->addItem( nameLayout, 0, 0 );
	mNameLabel = new QLabel( this );
	nameLayout->addRow( new QLabel( "Name:", this ), mNameLabel );

	// id
	QFormLayout* idLayout = new QFormLayout();
	layout->addItem( idLayout, 0, 1 );
	mIdLabel = new QLabel( this );
	idLayout->addRow( new QLabel( "\tid:", this ), mIdLabel );

	// email
	QFormLayout* eMailLayout = new QFormLayout();
	layout->addItem( eMailLayout, 1, 0 );
	mEMailEdit = new QLineEdit( this );
	connect( mEMailEdit, SIGNAL( textEdited( const QString& ) ), this, SLOT( setEMail( const QString& ) ) );
	eMailLayout->addRow( new QLabel( "eMail:", this ), mEMailEdit );
	
	// phone
	QFormLayout* phoneLayout = new QFormLayout();
	layout->addItem( phoneLayout, 1, 1 );
	mPhoneEdit = new QLineEdit( this );
	mPhoneEdit->setInputMask( tr( "000-000-0000;_" ) );
	connect( mPhoneEdit, SIGNAL( textEdited( const QString& ) ), this, SLOT( setPhone( const QString& ) ) );
	phoneLayout->addRow( new QLabel( "\tPhone:", this ), mPhoneEdit );
/*
	QTextEdit* mAddressEdit;
	QLineEdit* mGradYearEdit;
	QLineEdit* mProgramEdit;
	QLineEdit* mSubDisplineEdit;*/
}

StudentEditor::~StudentEditor()
{
}

void
StudentEditor::editStudent( Student* student )
{
	mStudent = student;
	if( mStudent != NULL )
	{
		mNameLabel->setText( mStudent->name() );
		mIdLabel->setText( QString::number( mStudent->id() ) );
		mEMailEdit->setText( mStudent->eMail() );
		mPhoneEdit->setText( mStudent->phone() );
	}
	else
	{
		mNameLabel->setText( "" );
		mIdLabel->setText( "" );
		mEMailEdit->setText( "" );
		mPhoneEdit->setText( "" );
	}
}

void
StudentEditor::setEMail( const QString& eMail )
{
	if( mStudent != NULL )
	{
		mStudent->setEMail( eMail );
	}
}

void
StudentEditor::setPhone( const QString& phone )
{
	if( mStudent != NULL )
	{
		QString n;
		QStringList split = phone.split( "-", QString::SkipEmptyParts );
		for( int i = 0; i < split.size(); i++ )
		{
			n.append( split.at( i ) );
		}
		mStudent->setPhone( n );
	}
}

void
StudentEditor::setAddress( const QString& address )
{
}

void
StudentEditor::setGradYear( const QString& gradYear )
{
}
